/*
 * Copyright (c) 2011, form105 Heiko Kundlacz
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.form105.rm.server.ant.hotfolder;

import java.io.File;


/**
 * A hotfolder object which represents informations about the incoming file. The build filename is predefined and
 * is named like the standard build file ant is looking for. The workflow id is generated by a unique id as 
 * this is used often in the software lifecycle. For each incoming file a temporary build folder is generated. 
 * The unique id is generated from the file name and the last modified date. Therefor the inbound file shouldn't
 * change after a workflow object has been created.
 * @author heikok
 *
 */
public class HotfolderInboundObject implements InboundObject {
	
	private String buildFileName;
	private File inboundFile;
	private String tempFolderName;
	private String tempFolderUniqueName;

	private String hotfolderName;
	
	public HotfolderInboundObject(File inboundFile, String tempFolderName) {
		this.inboundFile = inboundFile;
		this.tempFolderName = tempFolderName;
		this.tempFolderUniqueName = tempFolderName + File.separator + getId();
		this.buildFileName = tempFolderUniqueName + File.separator + "build.xml";
	}
	
	/* (non-Javadoc)
	 * @see net.form105.rm.server.ant.IInboundObject#getId()
	 */
	public String getId() {
		long modDate = inboundFile.lastModified();
		return inboundFile.getName()+":"+modDate;
	}
	
	/* (non-Javadoc)
	 * @see net.form105.rm.server.ant.IInboundObject#getWorkflowId()
	 */
	public String getWorkflowId() {
		return "wf_"+getId();
	}
	
	public String getHotfolderName() {
		return hotfolderName;
	}
	
	public void setHotfolderName(String hotfolderName) {
		this.hotfolderName = hotfolderName;
	}
	
	public String getTempFolderName() {
		return tempFolderName;
	}
	
	/**
	 * Getting the final temp folder which is unique and has been generated with an id in its name
	 * @return
	 */
	public String getTempFolderUniqueName() {
		return tempFolderUniqueName;
	}
	
	/**
	 * Getting the name of the inbound file
	 * @return
	 */
	public String getInboundFileName() {
		return inboundFile.getPath();
	}
	
	/**
	 * Getting the file that has arrived
	 * @return
	 */
	public File getInboundFile() {
		return inboundFile;
	}
	
	/**
	 * Build file path in the temp folder
	 * @return
	 */
	public String getBuildFileName() {
		return buildFileName;
	}
	
	public String getGroupId() {
		return getHotfolderName();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof HotfolderInboundObject) {
			HotfolderInboundObject inObject = (HotfolderInboundObject) obj;
			if (inObject.getInboundFileName().equals(((HotfolderInboundObject) obj).getInboundFileName())) {
				return true;
			}
		}
		return false;
	}
	
	
	
	

}
